package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.bcel.internal.generic.NEW;

import ex.com.jm.food.entity.FoodCart;
import ex.com.jm.food.entity.Menu;
import ex.com.jm.food.service.UserCartService;
import ex.com.jm.food.service.UserMenuService;
import ex.com.jm.food.utils.JsonUtils;

public class UserMenuAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private Menu menu;
	private UserMenuService userMenuService;
	private UserCartService userCartService;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private String order;
	private String sort;
	private String comId;
	private String menuId;
	private String userId;
	private List<FoodCart> foodCarts=new ArrayList<FoodCart>();
	private List<Menu> menuList=new ArrayList<Menu>();
	private Map<String, String> company=new HashMap<String, String>();
	public Menu getMenu() {
		return menu;
	}
	public void setMenu(Menu menu) {
		this.menu = menu;
	}
	public UserMenuService getUserMenuService() {
		return userMenuService;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public void setUserMenuService(UserMenuService userMenuService) {
		this.userMenuService = userMenuService;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public String getComId() {
		return comId;
	}
	public void setComId(String comId) {
		this.comId = comId;
	}
	public Map<String, String> getCompany() {
		return company;
	}
	public void setCompany(Map<String, String> company) {
		this.company = company;
	}
	public String getMenuId() {
		return menuId;
	}
	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}

	public List<Menu> getMenuList() {
		return menuList;
	}
	public void setMenuList(List<Menu> menuList) {
		this.menuList = menuList;
	}
	public UserCartService getUserCartService() {
		return userCartService;
	}
	public void setUserCartService(UserCartService userCartService) {
		this.userCartService = userCartService;
	}
	public List<FoodCart> getFoodCarts() {
		return foodCarts;
	}
	public void setFoodCarts(List<FoodCart> foodCarts) {
		this.foodCarts = foodCarts;
	}
	/**
	 * 到达菜单列表
	 * @return
	 */
	public String menuList(){
		List<Map<String, String>> companys=this.getUserMenuService().getCompany(comId);
		 if(companys!=null){
			 company=companys.get(0);
		 }
		 menuList=this.getUserMenuService().getMenus(0, 5, comId, "total_num", "desc");
		 foodCarts=this.getUserCartService().getCarts(userId);
		 System.out.println(foodCarts.size());
		return "menuList";
	}

	/**
	 * 获取数据
	 */
	public void getMenus(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		int offset=(initPage-1)*pageRow;
		List<Menu> menus=this.getUserMenuService().getMenus(offset, pageRow, comId, order, sort);
		JSONArray jsonmenu=JSONArray.fromObject(menus);
		totalCount=this.getUserMenuService().menuCount(comId);
		JSONObject jo=JsonUtils.buildJson(jsonmenu, initPage, pageRow, totalCount);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(jo);
		out.print(jo);
		out.flush();
		out.close();
	}
	
	public void getMenuCart(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		menu=this.getUserMenuService().getMenu(menuId);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("menuId", menu.getMenuId());
		map.put("menuName", menu.getMenuName());
		map.put("noPrice", menu.getMenuNowPrice());
		JSONObject jo=JSONObject.fromObject(map);
		System.out.println(jo);
				
	}
	
}
